package com.example.testcoroutine

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.MaterialTheme.typography
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.glide.rememberGlidePainter

class ComposeActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                MyApp()
            }
        }
    }

    @Preview
    @Composable
    fun MyApp() {
        Scaffold(
            backgroundColor = Color(0xfff5f5f5),
            content = {
                BarkHomeContent()
            }
        )
    }

    @Composable
    fun BarkHomeContent(
        viewModel: MainViewModel = viewModel(MainViewModel::class.java, "aa")
    ) {
        val state = remember { mutableStateOf(viewModel.livedata.value) }
        LazyColumn(
            contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),
        ) {
            items(
                count = if (state.value != null) state.value!!.size else 0,
                itemContent = {
                    PuppyListItem(puppy = state.value!![it])
                })
        }
    }

    @Composable
    fun PuppyListItem(puppy: Puppy) {
        val title = remember { mutableStateOf(puppy.title) }
        Card(
            modifier = Modifier
                .padding(horizontal = 8.dp, vertical = 8.dp)
                .fillMaxWidth()
                .clickable(onClick = {
                    println("Clicked")
                    if (title.value.contains("Clicked")) {
                        title.value = title.value.split(",")[0]
                    } else {
                        title.value += ", Clicked"
                    }
                    puppy.title = title.value
                }),
            elevation = 2.dp,
            backgroundColor = if (title.value.contains("Clicked")) Color(0xFFC5C5C5) else Color.White,
            shape = RoundedCornerShape(corner = CornerSize(16.dp))
        ) {
            Row {
                PuppyImage(puppy)
                Column(
                    modifier = Modifier
                        .padding(16.dp)
                        .fillMaxWidth()
                        .align(Alignment.CenterVertically)
                ) {
                    Text(text = title.value, style = typography.h6)
                    Text(text = puppy.description, style = typography.caption)
                }
            }
        }

    }

    @Composable
    fun PuppyImage(puppy: Puppy) {
        Image(
//            painter = rememberCoilPainter(puppy.puppyImageUrl),
            painter = rememberGlidePainter(request = puppy.puppyImageUrl, fadeIn = true),
            contentDescription = "oh no, ${puppy.description}",
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .padding(8.dp)
                .size(84.dp)
                .clip(RoundedCornerShape(corner = CornerSize(16.dp)))
        )
    }

}